package container

import (
	"context"
	"fmt"
	"github.com/Unixeno/islands/cmd/connection"
	"github.com/Unixeno/islands/cmd/utils"
	"github.com/Unixeno/islands/pb"
	"github.com/spf13/cobra"
)

var signal = ""

func GetContainerKillCmd() *cobra.Command {
	containerKillCmd := &cobra.Command{
		Use:   "kill id",
		Short: "kill the container",
		Long:  `kill specific container in your islands`,
		Args:  cobra.MinimumNArgs(1),
		Run:   containerKillHandle,
	}

	containerKillCmd.Flags().StringVarP(&signal, "signal", "s", "", "Signal to send to the container")
	return containerKillCmd
}

func containerKillHandle(cmd *cobra.Command, args []string) {

	id := CheckNameOrId(args[0])

	r, err := connection.Client.KillContainer(context.Background(), &pb.KillContainerReq{
		Id:     id,
		Signal: signal,
	})

	if err != nil {
		fmt.Printf("Cannot kill the container for reason %v\n", err)
	} else {
		if !utils.PrintError(r) {
			fmt.Println(id + " killed")
		}
	}
}
